<!doctype html>
<html>
<head>
  <meta charset='utf-8'>
  <title>Conditional formatting - Handsontable</title>

  <!--
  Loading Handsontable (full distribution that includes all dependencies)
  -->
  <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/handsontable.css">
  <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/pikaday/pikaday.css">
  <script data-jsfiddle="common" src="../dist/pikaday/pikaday.js"></script>
  <script data-jsfiddle="common" src="../dist/moment/moment.js"></script>
  <script data-jsfiddle="common" src="../dist/zeroclipboard/ZeroClipboard.js"></script>
  <script data-jsfiddle="common" src="../dist/handsontable.js"></script>

  <!--
  Loading demo dependencies. They are used here only to enhance the examples on this page
  -->
  <link data-jsfiddle="common" rel="stylesheet" media="screen" href="css/samples.css?20140331">
  <script src="js/samples.js"></script>
  <script src="js/highlight/highlight.pack.js"></script>
  <link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
  <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">

  <!--
  Facebook open graph. Don't copy this to your project :)
  -->
  <meta property="og:title" content="Conditional formatting - Handsontable">
  <meta property="og:description"
        content="This demo shows how to use the cell type renderer feature to make some conditional formatting">
  <meta property="og:url" content="http://handsontable.com/demo/conditional.html">
  <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
  <meta property="og:image:type" content="image/png">
  <meta property="og:image:width" content="409">
  <meta property="og:image:height" content="164">
  <link rel="canonical" href="http://handsontable.com/demo/conditional.html">

  <!--
  Google Analytics for GitHub Page. Don't copy this to your project :)
  -->
  <script src="js/ga.js"></script>
</head>

<body>


<div class="wrapper">
  <div class="wrapper-row">
    <div id="global-menu-clone">
      <h1><a href="../index.html">Handsontable</a></h1>

    </div>

    <div id="container">
      <div class="columnLayout">

        <div class="rowLayout">
      <div class="descLayout">
        <div class="pad" data-jsfiddle="example1">
          <h2>Conditional formatting</h2>

          <p>This demo shows how to use the cell type renderer feature to make some conditional formatting:</p>

          <ol>
            <li>first row is <span style="font-weight: bold">read-only</span>, and formatted in <span
              style="color: green; font-weight: bold">green bold</span> text
            </li>
            <li>all cells in the Nissan column are written in <span style="font-style: italic">italic</span></li>
            <li>empty cells have <span style="background: silver">silver</span> background</li>
            <li>negative numbers are written in <span style="color: red">red</span></li>
          </ol>

          <div id="example1"></div>

          <style>
            .negative {
              color: red;
            }
          </style>

          <p>
            <button name="dump" data-dump="#example1" data-instance="hot1" title="Prints current data source to Firebug/Chrome Dev Tools">
              Dump data to console
            </button>
          </p>
        </div>
      </div>

      <div class="codeLayout">
        <div class="pad">
          <div class="jsFiddle">
            <button class="jsFiddleLink" data-runfiddle="example1">Edit in jsFiddle</button>
          </div>

          <script data-jsfiddle="example1">
            var data = [
                ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
                ['2008', -5, '', 12, 13],
                ['2009', '', -11, 14, 13],
                ['2010', '', 15, -12, 'readOnly']
              ],
              container,
              hot1;

            function firstRowRenderer(instance, td, row, col, prop, value, cellProperties) {
              Handsontable.renderers.TextRenderer.apply(this, arguments);
              td.style.fontWeight = 'bold';
              td.style.color = 'green';
              td.style.background = '#CEC';
            }

            function negativeValueRenderer(instance, td, row, col, prop, value, cellProperties) {
              Handsontable.renderers.TextRenderer.apply(this, arguments);

              // if row contains negative number
              if (parseInt(value, 10) < 0) {
                // add class "negative"
                td.className = 'negative';
              }

              if (!value || value === '') {
                td.style.background = '#EEE';
              }
              else {
                if (value === 'Nissan') {
                  td.style.fontStyle = 'italic';
                }
                td.style.background = '';
              }
            }
            // maps function to lookup string
            Handsontable.renderers.registerRenderer('negativeValueRenderer', negativeValueRenderer);

            container = document.getElementById('example1');
            hot1 = new Handsontable(container, {
              data: data,
              startRows: 5,
              startCols: 5,
              minSpareRows: 1,
              contextMenu: true,
              onSelection: function (row, col, row2, col2) {
                var meta = this.getCellMeta(row2, col2);

                if (meta.readOnly) {
                  this.updateSettings({fillHandle: false});
                }
                else {
                  this.updateSettings({fillHandle: true});
                }
              },
              cells: function (row, col, prop) {
                var cellProperties = {};

                if (row === 0 || this.instance.getData()[row][col] === 'readOnly') {
                  cellProperties.readOnly = true; // make cell read-only if it is first row or the text reads 'readOnly'
                }
                if (row === 0) {
                  cellProperties.renderer = firstRowRenderer; // uses function directly
                }
                else {
                  cellProperties.renderer = "negativeValueRenderer"; // uses lookup map
                }

                return cellProperties;
              }
            });
          </script>
        </div>
      </div>
    </div>

        <div class="footer-text">
        </div>
      </div>

    </div>

  </div>
</div>

<div id="outside-links-wrapper"></div>

</body>
</html>
